﻿using System;
using System.IO;
using System.Web;

/// <summary>
/// Summary description for RepositoryManager
/// </summary>
public class RepositoryManager
{
    #region const

    private const string RepositoryFolderName = "Repository";

    #endregion

    #region enum
    public enum Folders
    {
        News
    }
    #endregion

    #region PhysicalPath

    private static string GetPhysicalPath(string folderName)
    {
        var request = HttpContext.Current.Request;
        string path = (request.ApplicationPath != @"/") ? request.ApplicationPath + @"/" : @"/";
        path += RepositoryFolderName + "/" + folderName + "/";
        var di = new DirectoryInfo(request.MapPath((path)));
        if (!di.Exists)
            di.Create();
        return request.MapPath(path);
    }

    public static string GetPhysicalPath(Folders folder)
    {
        return GetPhysicalPath(folder.ToString());
    }

    #endregion

    #region VirtualPath

    private static string SitePath
    {
        get
        {
            HttpRequest request = HttpContext.Current.Request;
            return (request.ApplicationPath != @"/") ? request.ApplicationPath + @"/" : @"/";
        }
    }

    public static string FullSitePath()
    {
        HttpRequest request = HttpContext.Current.Request;
        return request.Url.Scheme + "://" + request.Url.Authority + SitePath;
    }

    public static string GetUrlAuthority()
    {
        HttpRequest request = HttpContext.Current.Request;
        string http = request.IsSecureConnection ? "https://" : "http://";
        return http + request.Url.Authority;
    }

    private static string GetVirtualPath(Folders folder)
    {
        return SitePath + RepositoryFolderName + "/" + Enum.GetName(typeof(Folders), folder) + "/";
    }

    public static string GetVirtualPath(Folders folder, bool createIfNotExists)
    {
        string path = GetVirtualPath(folder);
        if (createIfNotExists)
        {
            HttpRequest request = HttpContext.Current.Request;
            var di = new DirectoryInfo(request.MapPath((path)));
            if (!di.Exists)
                di.Create();
        }
        //return GetUrlAuthority() + path;
        return path;
    }
    
    #endregion

}